﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using UcAsp.Net.Interface;
namespace UcAsp.Net.BLL
{
    public class SubjectsConfig
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        private static readonly ICache cache = DALFactory.CacheAccess.CreateCache();
        /// <summary>
        /// 获取产品
        /// </summary>
        public static List<UcAsp.Net.Model.Subjects> GetListSubjects(string theme)
        {

            if (cache.GetCache("GetListSubjects") == null)
            {
                List<UcAsp.Net.Model.Subjects> list = new List<UcAsp.Net.Model.Subjects>();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\Subjects.xml");
                string xmlpath = ("root/Subject");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {
                        UcAsp.Net.Model.Subjects model = new UcAsp.Net.Model.Subjects();

                        foreach (XmlNode xn in xns)
                        {
                            XmlToObject(model, xn.Name, xn.InnerText);
                        }
                        list.Add(model);
                    }
                }
                cache.CreateCache(list, "GetListSubjects" + theme);
            }
            return (List<UcAsp.Net.Model.Subjects>)cache.GetCache("GetListSubjects" + theme);


        }
        protected static void XmlToObject(object oPut, string name, string value)
        {
            System.ComponentModel.PropertyDescriptor pd = System.ComponentModel.TypeDescriptor.GetProperties(oPut).Find(name, true);
            if (pd != null)
            {
                if (pd.PropertyType == typeof(int) || pd.PropertyType == typeof(long) || pd.PropertyType == typeof(string) || pd.PropertyType == typeof(DateTime) || pd.PropertyType == typeof(bool))
                {
                    if (pd.PropertyType == typeof(int))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt32(value));
                    }
                    else if (pd.PropertyType == typeof(long))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt64(value));
                    }
                    else if (pd.PropertyType == typeof(double))
                    {
                        pd.SetValue(oPut, System.Convert.ToDouble(value));
                    }
                    else if (pd.PropertyType == typeof(bool))
                    {
                        pd.SetValue(oPut, System.Convert.ToBoolean(value));
                    }
                    else
                    {
                        if (value == null)
                            pd.SetValue(oPut, string.Empty);
                        else
                            pd.SetValue(oPut, value);
                    }
                }
            }
            else
            {
                //对象为空
            }
        }
        public static UcAsp.Net.Model.Subjects GetSubjects(int id, string theme)
        {
            UcAsp.Net.Model.Subjects product = GetListSubjects(theme).FirstOrDefault(o => o.SubjectId == id);
            return product;
        }
        public static UcAsp.Net.Model.Subjects GetSubjects(int id, int cid, string theme)
        {
            UcAsp.Net.Model.Subjects product = GetCategoriesSubjects(cid, theme).FirstOrDefault(o => o.SubjectId == id);
            return product;
        }
        public static UcAsp.Net.Model.Subjects GetMessageSubjects(int id, int cid, string theme)
        {
            UcAsp.Net.Model.Subjects product = GetMessageSubjects(cid, theme).FirstOrDefault(o => o.SubjectId == id);
            return product;
        }

        /// <summary>
        /// 分类页面主题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<UcAsp.Net.Model.Subjects> GetCategoriesSubjects(int id, string theme)
        {
            if (cache.GetCache("GetCategoriesSubjects" + theme + id.ToString()) == null)
            {
                List<UcAsp.Net.Model.Subjects> list = new List<UcAsp.Net.Model.Subjects>();
                if (!File.Exists(path + "\\themes\\" + theme + "\\config\\Categories\\" + id.ToString() + "\\Subjects.xml"))
                {
                    return list;
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\Categories\\" + id.ToString() + "\\Subjects.xml");
                string xmlpath = ("root/Subject");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {
                        UcAsp.Net.Model.Subjects model = new UcAsp.Net.Model.Subjects();

                        foreach (XmlNode xn in xns)
                        {
                            XmlToObject(model, xn.Name, xn.InnerText);
                        }
                        list.Add(model);
                    }
                }
                cache.CreateCache(list, "GetCategoriesSubjects" + theme + id.ToString());

            }
            return (List<UcAsp.Net.Model.Subjects>)cache.GetCache("GetCategoriesSubjects" + theme + id.ToString());
        }

        public static List<UcAsp.Net.Model.Subjects> GetMessageSubjects(int id, string theme)
        {
            if (cache.GetCache("GetMessageSubjects" + theme + id.ToString()) == null)
            {
                List<UcAsp.Net.Model.Subjects> list = new List<UcAsp.Net.Model.Subjects>();
                if (!File.Exists(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id.ToString() + "\\Subjects.xml"))
                {
                    return list;
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id.ToString() + "\\Subjects.xml");
                string xmlpath = ("root/Subject");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {
                        UcAsp.Net.Model.Subjects model = new UcAsp.Net.Model.Subjects();

                        foreach (XmlNode xn in xns)
                        {
                            XmlToObject(model, xn.Name, xn.InnerText);
                        }
                        list.Add(model);
                    }
                }
                cache.CreateCache(list, "GetMessageSubjects" + theme + id.ToString());
            }
            return (List<UcAsp.Net.Model.Subjects>)cache.GetCache("GetMessageSubjects" + theme + id.ToString());
        }
        public static bool SaveSubjects(UcAsp.Net.Model.Subjects model, string theme)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\Subjects.xml");
            string xmlpath = ("root/Subject");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {
                if (xns["SubjectId"].InnerText == model.SubjectId.ToString())
                {
                    xns["SubjectName"].InnerText = model.SubjectName;
                    if (!string.IsNullOrEmpty(model.SubjectImg))
                    {
                        xns["SubjectImg"].InnerText = model.SubjectImg;
                    }
                    xns["Categories"].InnerText = model.Categories.ToString();
                    xns["CategoryName"].InnerText = model.CategoryName;
                    xns["Keywords"].InnerText = model.Keywords;
                    xns["MaxNum"].InnerText = model.MaxNum.ToString();
                    xns["WhoView"].InnerText = model.WhoView.ToString();

                    if (xns["Themes"] == null)
                    {
                        XmlElement elem = xmlDoc.CreateElement("Themes");
                        elem.InnerText = model.Themes.ToString();
                        xns.AppendChild(elem);
                    }
                    else
                    {
                        xns["Themes"].InnerText = model.Themes.ToString();
                    }
                }
            }
            xmlDoc.Save(path + "\\themes\\" + theme + "\\config\\Subjects.xml");
            cache.RemoveCache("GetListSubjects" + theme);
            cache.RemoveAllCache();
            return true;
        }
        public static bool DeleteSubjects(string SubjectId, string theme)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\Subjects.xml");
            string xmlpath = ("root/Subject");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {
                if (xns["SubjectId"].InnerText == SubjectId.ToString())
                {
                    xns.ParentNode.RemoveChild(xns);
                }
            }
            xmlDoc.Save(path + "\\themes\\" + theme + "\\config\\Subjects.xml");
            cache.RemoveCache("GetListSubjects" + theme);
            cache.RemoveAllCache();
            return true;
        }
        public static bool InsertSubject(UcAsp.Net.Model.Subjects model, string theme)
        {
            XmlDocument xmlDoc = new XmlDocument();
            if (!Directory.Exists(path + "\\themes\\" + theme + "\\config"))
            {
                Directory.CreateDirectory(path + "\\themes\\" + theme + "\\config");
            }
            if (!File.Exists(path + "\\themes\\" + theme + "\\config\\Subjects.xml"))
            {
                using (StreamWriter sw = File.CreateText(path + "\\themes\\" + theme + "\\config\\Subjects.xml"))
                {
                    sw.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");
                }
            }
            xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\Subjects.xml");
            string xmlpath = ("root");
            XmlNode xmlnode = xmlDoc.SelectSingleNode(xmlpath);
            XmlElement subject = xmlDoc.CreateElement("Subject");
            XmlNode smallnote = xmlnode.AppendChild(subject);

            XmlElement subjectId = xmlDoc.CreateElement("SubjectId");
            subjectId.InnerText = model.SubjectId.ToString();
            smallnote.AppendChild(subjectId);


            XmlElement themes = xmlDoc.CreateElement("Themes");
            themes.InnerText = model.Themes.ToString();
            smallnote.AppendChild(themes);

            XmlElement subjectname = xmlDoc.CreateElement("SubjectName");
            subjectname.InnerText = model.SubjectName.ToString();
            smallnote.AppendChild(subjectname);

            XmlElement categories = xmlDoc.CreateElement("Categories");
            categories.InnerText = model.Categories.ToString();
            smallnote.AppendChild(categories);

            XmlElement categoryname = xmlDoc.CreateElement("CategoryName");
            categoryname.InnerText = model.CategoryName.ToString();
            smallnote.AppendChild(categoryname);

            XmlElement keywords = xmlDoc.CreateElement("Keywords");
            keywords.InnerText = model.Keywords;
            smallnote.AppendChild(keywords);

            XmlElement maxnum = xmlDoc.CreateElement("MaxNum");
            maxnum.InnerText = model.MaxNum.ToString();
            smallnote.AppendChild(maxnum);

            XmlElement whoview = xmlDoc.CreateElement("WhoView");
            whoview.InnerText = model.WhoView.ToString();
            smallnote.AppendChild(whoview);
            xmlDoc.Save(path + "\\themes\\" + theme + "\\config\\Subjects.xml");
            cache.RemoveCache("GetListSubjects" + theme);
            cache.RemoveAllCache();
            return true;
        }




        public static bool SaveCategoriesSubjects(UcAsp.Net.Model.Subjects model, string id, string theme)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\Categories\\" + id + "\\Subjects.xml");
            string xmlpath = ("root/Subject");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
            foreach (XmlNode xns in xmlList)
            {
                if (xns["SubjectId"].InnerText == model.SubjectId.ToString())
                {
                    xns["SubjectName"].InnerText = model.SubjectName;
                    if (!string.IsNullOrEmpty(model.SubjectImg))
                    {
                        xns["SubjectImg"].InnerText = model.SubjectImg;
                    }

                    xns["Categories"].InnerText = model.Categories.ToString();
                    xns["CategoryName"].InnerText = model.CategoryName;
                    xns["Keywords"].InnerText = model.Keywords;
                    xns["MaxNum"].InnerText = model.MaxNum.ToString();
                    xns["WhoView"].InnerText = model.WhoView.ToString();
                    if (xns["Themes"] == null)
                    {
                        XmlElement elem = xmlDoc.CreateElement("Themes");
                        elem.InnerText = model.Themes.ToString();
                        xns.AppendChild(elem);
                    }
                    else
                    {
                        xns["Themes"].InnerText = model.Themes.ToString();
                    }
                }
            }
            cache.RemoveCache("GetCategoriesSubjects" + theme + id);
            xmlDoc.Save(path + "\\themes\\" + theme + "\\config\\Categories\\" + id + "\\Subjects.xml");
            cache.RemoveAllCache();
            return true;
        }
        public static bool DeleteCategoriesSubjects(string SubjectId, string id, string theme)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\Categories\\" + id + "\\Subjects.xml");
            string xmlpath = ("root/Subject");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {
                if (xns["SubjectId"].InnerText == SubjectId.ToString())
                {
                    xns.ParentNode.RemoveChild(xns);
                }
            }
            xmlDoc.Save(path + "\\themes\\" + theme + "\\config\\Categories\\" + id + "\\Subjects.xml");
            cache.RemoveCache("GetCategoriesSubjects" + theme + id);
            cache.RemoveAllCache();
            return true;
        }
        public static bool InsertCategoriesSubjects(UcAsp.Net.Model.Subjects model, string id, string theme)
        {
            XmlDocument xmlDoc = new XmlDocument();
            if (!Directory.Exists(path + "\\themes\\" + theme + "\\config\\Categories\\" + id))
            {
                Directory.CreateDirectory(path + "\\themes\\" + theme + "\\config\\Categories\\" + id);
            }
            if (!File.Exists(path + "\\themes\\" + theme + "\\config\\Categories\\" + id + "\\Subjects.xml"))
            {
                using (StreamWriter sw = File.CreateText(path + "\\themes\\" + theme + "\\config\\Categories\\" + id + "\\Subjects.xml"))
                {
                    sw.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");
                }
            }
            xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\Categories\\" + id + "\\Subjects.xml");
            string xmlpath = ("root");
            XmlNode xmlnode = xmlDoc.SelectSingleNode(xmlpath);
            XmlElement subject = xmlDoc.CreateElement("Subject");
            XmlNode smallnote = xmlnode.AppendChild(subject);

            XmlElement subjectId = xmlDoc.CreateElement("SubjectId");
            subjectId.InnerText = model.SubjectId.ToString();
            smallnote.AppendChild(subjectId);


            XmlElement themes = xmlDoc.CreateElement("Themes");
            themes.InnerText = model.Themes.ToString();
            smallnote.AppendChild(themes);

            XmlElement subjectname = xmlDoc.CreateElement("SubjectName");
            subjectname.InnerText = model.SubjectName.ToString();
            smallnote.AppendChild(subjectname);

            XmlElement categories = xmlDoc.CreateElement("Categories");
            categories.InnerText = model.Categories.ToString();
            smallnote.AppendChild(categories);

            XmlElement categoryname = xmlDoc.CreateElement("CategoryName");
            categoryname.InnerText = model.CategoryName.ToString();
            smallnote.AppendChild(categoryname);

            XmlElement keywords = xmlDoc.CreateElement("Keywords");
            keywords.InnerText = model.Keywords;
            smallnote.AppendChild(keywords);

            XmlElement maxnum = xmlDoc.CreateElement("MaxNum");
            maxnum.InnerText = model.MaxNum.ToString();
            smallnote.AppendChild(maxnum);

            XmlElement whoview = xmlDoc.CreateElement("WhoView");
            whoview.InnerText = model.WhoView.ToString();
            smallnote.AppendChild(whoview);
            xmlDoc.Save(path + "\\themes\\" + theme + "\\config\\Categories\\" + id + "\\Subjects.xml");
            cache.RemoveCache("GetCategoriesSubjects" + theme + id);
            cache.RemoveAllCache();
            return true;
        }



        public static bool SaveMessageSubjects(UcAsp.Net.Model.Subjects model, string id, string theme)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id + "\\Subjects.xml");
            string xmlpath = ("root/Subject");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {
                if (xns["SubjectId"].InnerText == model.SubjectId.ToString())
                {
                    xns["SubjectName"].InnerText = model.SubjectName;
                    if (!model.SubjectImg.Equals("nofile"))
                    {
                        xns["SubjectImg"].InnerText = model.SubjectImg;
                    }
                    xns["Categories"].InnerText = model.Categories.ToString();
                    xns["CategoryName"].InnerText = model.CategoryName;
                    xns["Keywords"].InnerText = model.Keywords;
                    xns["MaxNum"].InnerText = model.MaxNum.ToString();
                    xns["WhoView"].InnerText = model.WhoView.ToString();
                    if (xns["Themes"] == null)
                    {
                        XmlElement elem = xmlDoc.CreateElement("Themes");
                        elem.InnerText = model.Themes.ToString();
                        xns.AppendChild(elem);
                    }
                    else
                    {
                        xns["Themes"].InnerText = model.Themes.ToString();
                    }
                }
            }
            cache.RemoveCache("GetMessageSubjects" + theme + id);
            xmlDoc.Save(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id + "\\Subjects.xml");
            cache.RemoveAllCache();
            return true;
        }
        public static bool DeleteMessageSubjects(string SubjectId, string id, string theme)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id + "\\Subjects.xml");
            string xmlpath = ("root/Subject");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {
                if (xns["SubjectId"].InnerText == SubjectId.ToString())
                {
                    xns.ParentNode.RemoveChild(xns);
                }
            }
            xmlDoc.Save(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id + "\\Subjects.xml");
            cache.RemoveCache("GetMessageSubjects" + theme + id);
            cache.RemoveAllCache();
            return true;
        }
        public static bool InsertMessageSubjects(UcAsp.Net.Model.Subjects model, string id, string theme)
        {
            XmlDocument xmlDoc = new XmlDocument();
            if (!Directory.Exists(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id))
            {
                Directory.CreateDirectory(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id);
            }
            if (!File.Exists(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id + "\\Subjects.xml"))
            {
                using (StreamWriter sw = File.CreateText(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id + "\\Subjects.xml"))
                {
                    sw.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");
                }
            }
            xmlDoc.Load(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id + "\\Subjects.xml");
            string xmlpath = ("root");
            XmlNode xmlnode = xmlDoc.SelectSingleNode(xmlpath);
            XmlElement subject = xmlDoc.CreateElement("Subject");
            XmlNode smallnote = xmlnode.AppendChild(subject);

            XmlElement subjectId = xmlDoc.CreateElement("SubjectId");
            subjectId.InnerText = model.SubjectId.ToString();
            smallnote.AppendChild(subjectId);


            XmlElement themes = xmlDoc.CreateElement("Themes");
            themes.InnerText = model.Themes.ToString();
            smallnote.AppendChild(themes);

            XmlElement subjectname = xmlDoc.CreateElement("SubjectName");
            subjectname.InnerText = model.SubjectName.ToString();
            smallnote.AppendChild(subjectname);

            XmlElement categories = xmlDoc.CreateElement("Categories");
            categories.InnerText = model.Categories.ToString();
            smallnote.AppendChild(categories);

            XmlElement categoryname = xmlDoc.CreateElement("CategoryName");
            categoryname.InnerText = model.CategoryName.ToString();
            smallnote.AppendChild(categoryname);

            XmlElement keywords = xmlDoc.CreateElement("Keywords");
            keywords.InnerText = model.Keywords;
            smallnote.AppendChild(keywords);

            XmlElement maxnum = xmlDoc.CreateElement("MaxNum");
            maxnum.InnerText = model.MaxNum.ToString();
            smallnote.AppendChild(maxnum);

            XmlElement whoview = xmlDoc.CreateElement("WhoView");
            whoview.InnerText = model.WhoView.ToString();
            smallnote.AppendChild(whoview);
            xmlDoc.Save(path + "\\themes\\" + theme + "\\config\\ShowMessage\\" + id + "\\Subjects.xml");
            cache.RemoveCache("GetMessageSubjects" + theme + id);
            cache.RemoveAllCache();
            return true;
        }
    }
}